package com.eques.demo.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.eques.bean.AnimalAlarmInfo
import com.eques.bean.AnimalMsgInfo
import com.eques.bean.WeekBean
import com.eques.demo.R
import com.eques.demo.adapter.viewhoder.AlarmHoder
import com.eques.demo.adapter.viewhoder.AnimalHoder
import com.eques.demo.adapter.viewhoder.MsgHoder
import com.eques.demo.adapter.viewhoder.WeeKHoder
import com.eques.impl.OnClickListenerImpl
import java.util.*

class AnimalAdapter(
    var context: Context,
    var beans: List<Objects>? = listOf(),
    var adapterType: Int
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var WEEK_ADAPTER = 0
    private var FOOD_PLAN = 1
    private var ANIMAL_ALARM = 2
    private var ANIMAL_MSG = 3

    private var onItemClickListener: OnClickListenerImpl? = null

    fun setOnItemClickListener(onItemClickListener: OnClickListenerImpl?) {
        this.onItemClickListener = onItemClickListener
    }


    /**
     * 创建新的视图持有者
     *
     * 根据不同的适配器类型，inflate相应的布局并创建对应的视图持有者
     * 这个方法主要用于RecyclerView的适配器中，以支持不同类型的列表项布局
     *
     * @param parent 视图的父容器，用于确定布局参数
     * @param viewType 视图类型，根据类型决定布局样式
     * @return 返回根据视图类型创建的视图持有者对象
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var view: View?

        // 根据adapterType决定实例化的视图类型
        return when (adapterType) {
            WEEK_ADAPTER -> {
                // 对于周适配器类型，使用周布局
                view = LayoutInflater.from(context)
                    .inflate(R.layout.item_voice_set_week_layout, parent, false)
                WeeKHoder(view)
            }

            FOOD_PLAN -> {
                // 对于食物计划类型，使用食物计划布局
                view = LayoutInflater.from(context)
                    .inflate(R.layout.item_food_plan_layout, parent, false)
                AnimalHoder(view)
            }

            ANIMAL_ALARM -> {
                // 对于动物报警类型，使用动物报警布局
                view = LayoutInflater.from(context)
                    .inflate(R.layout.item_animal_alarm_layout, parent, false)
                AlarmHoder(view)
            }
            else -> {
                // 对于其他情况，默认使用动物报警布局
                view = LayoutInflater.from(context)
                    .inflate(R.layout.item_animal_alarm_layout, parent, false)
                MsgHoder(view)
            }
        }
    }

    override fun getItemCount(): Int {
        return beans!!.size
    }

    /**
     * 重写onBindViewHolder方法以根据不同的适配器类型绑定不同的视图持有者
     *
     * @param holder 视图持有者，用于展示条目视图
     * @param position 条目的位置，用于确定要展示的数据
     */
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        // 根据适配器类型选择相应的视图持有者和数据绑定逻辑
        when (adapterType) {
            WEEK_ADAPTER -> {
                // 将视图持有者转换为WeeKHoder类型，并为它设置数据
                var weeKHoder: WeeKHoder = holder as WeeKHoder
                weeKHoder.setData(context, beans as List<WeekBean>, position, onItemClickListener!!)
            }

            FOOD_PLAN -> {
                // 将视图持有者转换为AnimalHoder类型，并为它设置数据
                var foodHodel: AnimalHoder = holder as AnimalHoder
                foodHodel.setData(context, beans as List<String>, position, onItemClickListener!!)
            }

            ANIMAL_ALARM -> {
                // 将视图持有者转换为AlarmHoder类型，并为它设置数据
                var alarmHoder: AlarmHoder = holder as AlarmHoder
                alarmHoder.setData(context, beans as List<AnimalAlarmInfo>, position)
            }

            ANIMAL_MSG -> {
                // 将视图持有者转换为MsgHoder类型，并为它设置数据
                var alarmHoder: MsgHoder = holder as MsgHoder
                alarmHoder.setData(context, beans as List<AnimalMsgInfo>, position,onItemClickListener!!)
            }
        }
    }
}